﻿using EasyDDD.AdminModule.Domain.DomainEventsSysAdminRoleMenu;
using EasyDDD.Domain;
using EasyDDD.Domain.User;
using Microsoft.EntityFrameworkCore;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace EasyDDD.AdminModule.Domain.AggregateSysAdminRole
{
    /// <summary>
    /// 后台管理角色
    /// </summary>
    //public record SysAdminRoleId(Guid Value) : Key<Guid>(Value);
    //[Table(nameof(SysAdminRole))]
    public class SysAdminRole : ApplicationRole, IAggregateRoot
    {
        [JsonConstructor]
        protected SysAdminRole() { }

        #region 聚合 SysAdminRoleMenu
        public virtual ICollection<SysAdminRoleMenu> SysAdminRoleMenuList { get; private set; } = new Collection<SysAdminRoleMenu>();
       
        public void SetSysAdminRoleMenu(Guid sysMenuId)
        {
            if (!this.SysAdminRoleMenuList.Any(e => e.SysMenuId == sysMenuId))
            {
                var item = new SysAdminRoleMenu(this.Id, sysMenuId);
                this.SysAdminRoleMenuList.Add(item);
                //this.AddDomainEvent(new SysAdminRoleMenuAddedDomainEvent(item));
            }
        }

        public void RemoveSysAdminRoleMenu(SysAdminRoleMenu item)
        {
            this.SysAdminRoleMenuList.Remove(item);
            //item.AddDomainEvent(new SysAdminRoleMenuRemovedDomainEvent(item));
        }

        public void SetSysMenuRuleList(Guid[] sysMenuIds)
        {
            foreach (var item in this.SysAdminRoleMenuList.Where(e => !sysMenuIds.Contains(e.SysMenuId)).ToList())
            {
                RemoveSysAdminRoleMenu(item);
            }

            foreach (var sysMenuId in sysMenuIds)
            {
                this.SetSysAdminRoleMenu(sysMenuId);
            }
            this.AddDomainEvent(new SysAdminRoleSetMenuListDomainEvent(this));
        } 
        #endregion
    }
}
